package com.baimifan.renovation.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.model.TaskClassify;
import com.baimifan.renovation.view.TaskClassifyHolder;

import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/04.
 */

public class TaskClassifyAdapter extends RecyclerView.Adapter<TaskClassifyHolder> {
    private ArrayList<TaskClassify> mlists;
    private Float w, h;

    private OnItemClickListener itemClickListener;

    public TaskClassifyAdapter (ArrayList<TaskClassify> lists, Float w, Float h) {
        this.mlists = lists;
        this.w = w;
        this.h = h;
    }
    @Override
    public TaskClassifyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_classify_item, parent, false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        TaskClassifyHolder taskClassify = new TaskClassifyHolder(view);
        return taskClassify;
    }

    @Override
    public void onBindViewHolder(TaskClassifyHolder holder, final int position) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)(w*106), ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins((int)(w*9),0,(int)(w*9),0);
        if (mlists.size() > 0) {
            if (position == 0) {
                params.setMargins((int)(w*12),0,(int)(w*9),0);
            } else if (position == mlists.size() - 1) {
                params.setMargins(0,0,(int)(w*12),0);
            } else {
                params.setMargins(0,0,(int)(w*9),0);
            }
        }
        holder.iv.setLayoutParams(params);
        x.image().bind(holder.iv, mlists.get(position).getImg());
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.OnItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlists.size();
    }


    public interface OnItemClickListener {
        void OnItemClick (int position);
    }

    public void setOnItemListener (OnItemClickListener onItemListener) {
        this.itemClickListener = onItemListener;
    }
}
